package cat.sun.stats;

import java.util.ArrayList;
import java.util.List;

import cat.sun.R;

import android.content.SharedPreferences;
import android.content.res.Resources;


public class Stats 
{
	
	private SharedPreferences settings;
	private String type;
	private Resources resources;
	
	public Stats(SharedPreferences settings,String type,Resources res)
	{
		this.type = type;
		this.settings = settings;
		this.resources = res;
	}
	
	public  List<Score> GetBestScores()
	{		
		float [] temp = new float[StatsValues.MAX_IN_LIST]; 
		for (int i = 0;i<StatsValues.MAX_IN_LIST;i++)
		{
			temp[i] = settings.getFloat("N"+Integer.toString(i), -1);
		}
		int k = 0;
		for (int i = 0;i<StatsValues.MAX_IN_LIST;i++)
		{
			if (temp[i]!=-1) k++;
		}
		List<Score> score = new ArrayList<Score>();
		if (k==0) return score;
		float [] res = new float[k];
		for (int i=0;i<k;i++)
		{
			res[i] = temp[i];
		}
		for (int i=0;i<k;i++)
		{
			score.add(new Score(settings.getString("P"+Integer.toString(i), "Player"),temp[i]));
		}
		return score;	
	}
	
	public List<Score> AddToList(List<Score> mas,Score res)
	{
		int pos = mas.size();
		for (int i = 0;i<mas.size();i++)
		{
			if (mas.get(i).getScore() > res.getScore())
			{
				pos = i;
				break;
			}
		}
		mas.add(pos, res);
		while (mas.size()>StatsValues.MAX_IN_LIST) mas.remove(mas.size()-1);
		return mas;
	}
	
	public void AddToStats(List<Score> mas)
	{
		for (int i = 0; i < mas.size();i++)
		{
			SharedPreferences.Editor editor =  this.settings.edit();
			editor.putFloat("N"+Integer.toString(i), mas.get(i).getScore());
			editor.putString("P"+Integer.toString(i), mas.get(i).getName());
			editor.commit();
		}
	}
	
	public boolean IsHeInTop(List<Score> score, float res)
	{
		if (type.equals(StatsValues.CUSTOM_KEY) || type.equals(StatsValues.RANDOM_KEY)) return false;
		boolean b = false;
		if (score.size()<5) b = true; else 
		{
			if (score.get(score.size()-1).getScore()>(res/1000)) b = true;
		}
		return b;
	}
	
	public String LeadersBoard(List<Score> score)
	{
		String res = "";
		for (int i=0;i<score.size();i++)
		{
			if (i!=score.size()-1) res += Integer.toString(i+1)+". "+score.get(i).getName() + ", Time -  " + Float.toString(score.get(i).getScore()) + "\n";
			else res += Integer.toString(i+1)+". "+score.get(i).getName() + ", Time -  " + Float.toString(score.get(i).getScore());
		}
		return res;
	}
	
	public List<StringScore> getStringScores()
	{
		List<StringScore> res = new ArrayList<StringScore>();
		List<Score> real = GetBestScores();
		for (int i=0;i<real.size();i++)
			res.add(new StringScore(real.get(i).getName(), Float.toString(real.get(i).getScore())));
		for (int i = real.size();i<StatsValues.MAX_IN_LIST;i++)
			res.add(new StringScore(resources.getString(R.string.emptyPlayer), resources.getString(R.string.emptyScore)));
		return res;
	}
	
	public String getType()
	{
		return this.type;
	}
	
	
}
